using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using jQueryApi;

namespace Nsb.Classes
{
    [IgnoreNamespace]
    public class TrueForm
    {
        public void FillFormByName()
        {
            foreach (KeyValuePair<string, object> p in (Dictionary)(object)this)
            {
                if (Type.GetScriptType(p.Value) != "function")
                    jQuery.Select("input[name=" + p.Key + "]").Value(p.Value.ToString());
            }
        }

        public void ScoopFormByName()
        {
            Dictionary data = (Dictionary)(object)this;

            foreach (KeyValuePair<string, object> p in data)
            {
                if (Type.GetScriptType(p.Value) != "function")
                {
                    switch (Type.GetScriptType(data[p.Key]))
                    {
                        case "boolean":
                            data[p.Key] = Boolean.Parse(jQuery.Select("input[name=" + p.Key + "]").GetValue());
                            break;
                        case "number":
                            data[p.Key] = Number.ParseInt(jQuery.Select("input[name=" + p.Key + "]").GetValue());
                            break;
                        default:
                            data[p.Key] = jQuery.Select("input[name=" + p.Key + "]").GetValue();
                            break;
                    }
                }
            }
        }

        public void FillFormByClass()
        {
            foreach (KeyValuePair<string, object> p in (Dictionary)(object)this)
            {
                string classKey = p.Key.Substr(0, 1).ToUpperCase() + p.Key.Substr(1);
                if (Type.GetScriptType(p.Value) != "function")
                    jQuery.Select("." + classKey).Plugin<Jewl>().FValueString(p.Value.ToString());
            }
        }

        public void ScoopFormByClass()
        {
            Dictionary data = (Dictionary)(object)this;

            foreach (KeyValuePair<string, object> p in data)
            {
                string classKey = p.Key.Substr(0, 1).ToUpperCase() + p.Key.Substr(1);
                if (Type.GetScriptType(p.Value) != "function")
                {
                    switch (Type.GetScriptType(data[p.Key]))
                    {
                        case "boolean":
                            data[p.Key] = Boolean.Parse(jQuery.Select("." + classKey).Plugin<Jewl>().GetFValueString());
                            break;
                        case "number":
                            data[p.Key] = Number.ParseInt(jQuery.Select("." + classKey).Plugin<Jewl>().GetFValueString());
                            break;
                        default:
                            data[p.Key] = jQuery.Select("." + classKey).Plugin<Jewl>().GetFValueString();
                            break;
                    }
                }
            }
        }

    }
}
